package com.xique.door.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.api.request.build.RemoteHouseListRequest;
import com.xique.door.api.request.build.RemoteHouseUserListRequest;
import com.xique.door.bean.domain.BuildHouseUserV1;
import com.xique.door.bean.domain.BuildHouseV1;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.search.build.HouseListSearch;
import com.xique.door.bean.search.build.HouseUserListSearch;
import com.xique.door.bean.vo.area.HouseListVO;
import com.xique.door.bean.vo.area.HouseUserListV1VO;
import com.xique.door.biz.service.IBuildHouseBizService;
import com.xique.door.service.IBuildHouseService;
import com.xique.door.service.IBuildHouseUserService;
import com.xique.door.service.IUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2022/3/9 10:51
 */
@Service
public class BuildHouseBizServiceImpl implements IBuildHouseBizService {

    @Autowired
    private IBuildHouseService houseService;

    @Autowired
    private IBuildHouseUserService houseUserService;

    @Autowired
    private IUserService userService;

    @Override
    public AjaxResult selectHouseList(RemoteHouseListRequest request) {

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", new ArrayList<>());
        map.put("total", 0);

        HouseListSearch search = OrikaUtil.convert(request, HouseListSearch.class);
        search.setParkId(request.getProjectId());
        IPage<BuildHouseV1> page = houseService.selectHousePageV1(search);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return AjaxResult.success(map);
        }

        List<HouseListVO> vos = OrikaUtil.converts(page.getRecords(), HouseListVO.class);

        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult selectHouseUserList(RemoteHouseUserListRequest request) {

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", new ArrayList<>());
        map.put("total", 0);

        HouseUserListSearch search = OrikaUtil.convert(request, HouseUserListSearch.class);
        IPage<BuildHouseUserV1> page = houseUserService.selectHouseUserPageV1(search);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return AjaxResult.success(map);
        }
        List<BuildHouseUserV1> list = page.getRecords();
        List<Long> userIds = list.stream().map(BuildHouseUserV1::getUserId).distinct().collect(Collectors.toList());
        List<User> userList = userService.selectUserByIds(userIds);
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, o -> o));

        List<Long> houseIds = list.stream().map(BuildHouseUserV1::getHouseId).distinct().collect(Collectors.toList());
        List<BuildHouseV1> houseList = houseService.listByIds(houseIds);
        Map<Long, BuildHouseV1> houseMap = houseList.stream().collect(Collectors.toMap(BuildHouseV1::getId, o -> o));

        List<HouseUserListV1VO> vos = OrikaUtil.converts(list, HouseUserListV1VO.class);
        vos.forEach(item -> {
            if (StringUtils.isNotNull(userMap.get(item.getUserId()))) {
                item.setUserName(userMap.get(item.getUserId()).getUserName());
            }
            if (StringUtils.isNotNull(houseMap.get(item.getHouseId()))) {
                item.setHouseName(houseMap.get(item.getHouseId()).getHouseName());
            }
        });

        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }
}
